<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
{% extends 'base.html' %}
{% load staticfiles %}
{% block title %}Chungo书城-商品详情{% endblock title %}
{% block header_con %}{% endblock header_con %}
{% block search_bar %}{% endblock search_bar %}
{% block topfiles %}
    <script>
        $(function () {
            update_total_price()// 计算总价
            function update_total_price() {
                books_price = $('.show_pirze').children('em').text() // 获取商品的价格和数量
                books_count = $('.num_show').val()
                books_price = parseFloat(books_price) // 计算商品的总价
                books_count = parseInt(books_count)
                total_price = books_price * books_count
                $('.total').children('em').text(total_price.toFixed(2) + '元')// 设置商品总价
            }

            $('.add').click(function () { // 商品增加
                books_count = $('.num_show').val()  // 获取商品的数量
                books_count = parseInt(books_count) + 1 // 加1
                $('.num_show').val(books_count)// 重新设置值
                update_total_price() // 计算总价
            })
            $('.minus').click(function () {  // 商品减少
                books_count = $('.num_show').val() // 获取商品的数量
                books_count = parseInt(books_count) - 1   // 加1
                if (books_count == 0) {
                    books_count = 1
                }
                $('.num_show').val(books_count) // 重新设置值
                update_total_price()// 计算总价
            })
            $('.num_show').blur(function () { // 手动输入
                books_count = $(this).val()   // 获取商品的数量
                if (isNaN(books_count) || books_count.trim().length == 0 || !(parseInt(books_count) > 0)) { // 数据校验
                    books_count = 1
                }
                $('.num_show').val(parseInt(books_count))   // 重新设置值
                update_total_price()   // 计算总价
            })
        })
    </script>
{% endblock topfiles %}
{% block body %}
    <div class="header_con">
        <div class="header">
            <div class="welcome fl">欢迎来到尚硅谷!</div>
            <div class="fr">
                {% if request.session.islogin %}
                    <div class="login_btn fl">
                        欢迎您：<em>{{ request.session.username }}</em>
                        <span>|</span>
                        <a href="{% url 'user:logout' %}">退出</a>
                    </div>
                {% else %}
                    <div class="login_btn fl">
                        <a href="{% url 'user:login' %}">登录</a>
                        <span>|</span>
                        <a href="{% url 'user:register' %}">注册</a>
                    </div>
                {% endif %}
                <div class="user_link fl">
                    <span>|</span>
                    <a href="user_center_info.html">用户中心</a>
                    <span>|</span>
                    <a href="cart.html">我的购物车</a>
                    <span>|</span>
                    <a href="user_center_order.html">我的订单</a>
                </div>
            </div>
        </div>
    </div>

    <div class="search_bar clearfix">
        <a href="index.html" class="logo fl"><img src="{% static 'images/logo.png' %}"
                                                  style="width: 160px; height: 53px;"></a>
        <div class="search_con fl">
            <input type="text" class="input_text fl" name="" placeholder="搜索商品">
            <input type="button" class="input_btn fr" name="" value="搜索">
        </div>
        <div class="guest_cart fr">
            <a href="{% url "cart:show" %}" class="cart_name fl">我的购物车</a>
            <div class="book_count fl" id="show_count">1</div>
        </div>
    </div>

    <div class="navbar_con">
        <div class="navbar clearfix">
            <div class="subnav_con fl">
                <h1>全部商品分类</h1>
                <span></span>
                <ul class="subnav">
                    {{ type_title }}
                    <li><a href="#" class="python">Python</a></li>
                    <li><a href="#" class="javascript">Javascript</a></li>
                    <li><a href="#" class="algorithms">数据结构与算法</a></li>
                    <li><a href="#" class="machinelearning">机器学习</a></li>
                    <li><a href="#" class="operatingsystem">操作系统</a></li>
                    <li><a href="#" class="database">数据库</a></li>
                </ul>
            </div>
            <ul class="navlist fl">
                <li><a href="">首页</a></li>
                <li class="interval">|</li>
                <li><a href="">移动端书城</a></li>
                <li class="interval">|</li>
                <li><a href="">秒杀</a></li>
            </ul>
        </div>
    </div>

    <div class="breadcrumb">
        <a href="#">全部分类</a>
        <span>></span>
        <a href="#">Python</a>
        <span>></span>
        <a href="#">商品详情</a>
    </div>

    <div class="book_detail_con clearfix">
        <div class="book_detail_pic fl"><img src="{% static books.image.url %}"></div>

        <div class="book_detail_list fr">
            <h3>{{ books.name }}</h3>
            <p>{{ books.desc }}</p>
            <div class="prize_bar">
                <span class="show_pirze">¥<em>{{ books.price }}</em></span>
                <span class="show_unit">单  位：{{ books.unit }}</span>
            </div>
            <div class="book_num clearfix">
                <div class="num_name fl">数 量：</div>
                <div class="num_add fl">
                    <input type="text" class="num_show fl" value="1">
                    <a href="javascript:;" class="add fr">+</a>
                    <a href="javascript:;" class="minus fr">-</a>
                </div>
            </div>
            <div class="total">总价：<em>100元</em></div>
            <div class="operate_btn">
                {% csrf_token %}
                <a href="javascript:;" class="buy_btn">立即购买</a>
                <a href="javascript:;" books_id="{{ books.id }}" class="add_cart" id="add_cart">加入购物车</a>
            </div>
             {% csrf_token %}
            <a href="{% url "comments:comment" books_id=books.id %}" id="write-comment" class="comment">我要写评论</a>
            <div style="display:flex;" id="comment-input" data-bookid="{{ books.id }}"
                 data-userid="{{ request.session.passport_id }}">
                <div>
                    <input type="text" placeholder="评论内容">
                </div>
                <div id="submit-comment">
                    <button>
                        提交评论
                    </button>
                </div>
            </div>
        </div>
    </div>

    <div class="main_wrap clearfix">
        <div class="l_wrap fl clearfix">
            <div class="new_book">
                <h3>新品推荐</h3>
                <ul>
                    {% for book in books_li %}
                        <li>
                            <a href="{% url 'books:detail' books_id=books.id %}"><img
                                    src="{% static books.image.url %}"></a>
                            <h4><a href="{% url 'books:detail' books_id=books.id %}">{{ book.name }}</a></h4>
                            <div class="prize">￥{{ book.price }}</div>
                        </li>
                    {% endfor %}
                </ul>
            </div>
        </div>

        <div class="r_wrap fr clearfix">
            <ul class="detail_tab clearfix">
                <li class="active">商品介绍</li>
                <li>评论</li>
            </ul>

            <div class="tab_content">
                <dl>
                    <dt>商品详情：</dt>
                    <dd>{{ books.detail | safe }}</dd>
                </dl>
            </div>

        </div>
    </div>

    <div class="add_jump"></div>

    <script type="text/javascript" src="js/jquery-1.12.2.js"></script>
    <script type="text/javascript">
        var $add_x = $('#add_cart').offset().top;
        var $add_y = $('#add_cart').offset().left;

        var $to_x = $('#show_count').offset().top;
        var $to_y = $('#show_count').offset().left;

        $('#add_cart').click(function () {
            // 获取商品的id和商品数量
            books_id = $(this).attr('books_id')
            books_count = $('.num_show').val()
            csrf = $('input[name="csrfmiddlewaretoken"]').val()
            // 发起请求，访问/cart/add/, 进行购物车数据的添加
            params = {
                'books_id': books_id,
                'books_count': books_count,
                'csrfmiddlewaretoken': csrf
            }

            $.post('/cart/add/', params, function (data) {
                if (data.res == 5) {
                    // 添加成功
                    $(".add_jump").css({'left': $add_y + 80, 'top': $add_x + 10, 'display': 'block'})
                    $(".add_jump").stop().animate({
                            'left': $to_y + 7,
                            'top': $to_x + 7
                        },
                        "fast", function () {
                            $(".add_jump").fadeOut('fast', function () {
                                // 获取原有show_count的值
                                count = $('#show_count').html()
                                count = parseInt(count) + parseInt(books_count)
                                $('#show_count').html(count);
                            });
                        });
                } else {
                    // 添加失败
                    alert(data.errmsg)
                }
            })
        })
    </script>
{% endblock body %}
</html>